package com.zenithmind.library.pojo.vo;

import com.fasterxml.jackson.annotation.JsonFormat;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
import org.springframework.format.annotation.DateTimeFormat;

import java.time.LocalDateTime;

/**
 * 图书评价视图对象
 * 
 * @author ZenithMind
 * @since 2024-06-14
 */
@Data
@Schema(name = "图书评价视图对象", description = "图书评价信息的视图对象")
public class BookReviewVO {

    @Schema(description = "评价ID")
    private String id;

    @Schema(description = "用户ID")
    private String userId;

    @Schema(description = "用户姓名")
    private String userName;

    @Schema(description = "用户头像")
    private String userAvatar;

    @Schema(description = "图书ID")
    private String bookId;

    @Schema(description = "图书标题")
    private String bookTitle;

    @Schema(description = "图书封面")
    private String bookCover;

    @Schema(description = "评分")
    private Integer rating;

    @Schema(description = "评分星级显示")
    private String ratingStars;

    @Schema(description = "评价内容")
    private String reviewContent;

    @Schema(description = "是否匿名")
    private Integer isAnonymous;

    @Schema(description = "状态")
    private Integer status;

    @Schema(description = "状态名称")
    private String statusName;

    @Schema(description = "点赞数")
    private Integer likeCount;

    @Schema(description = "创建时间")
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
    @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private LocalDateTime createTime;

    @Schema(description = "更新时间")
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
    @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private LocalDateTime updateTime;

    /**
     * 获取评分星级显示
     */
    public String getRatingStars() {
        if (rating == null) {
            return "☆☆☆☆☆";
        }
        StringBuilder stars = new StringBuilder();
        for (int i = 1; i <= 5; i++) {
            if (i <= rating) {
                stars.append("★");
            } else {
                stars.append("☆");
            }
        }
        return stars.toString();
    }

    /**
     * 获取状态名称
     */
    public String getStatusName() {
        if (status == null) {
            return "未知";
        }
        return switch (status) {
            case 0 -> "隐藏";
            case 1 -> "显示";
            default -> "未知";
        };
    }

    /**
     * 获取显示的用户名称
     */
    public String getDisplayUserName() {
        if (isAnonymous != null && isAnonymous == 1) {
            return "匿名用户";
        }
        return userName != null ? userName : "未知用户";
    }

    /**
     * 判断是否为好评
     */
    public boolean isPositiveReview() {
        return rating != null && rating >= 4;
    }

    /**
     * 判断是否为差评
     */
    public boolean isNegativeReview() {
        return rating != null && rating <= 2;
    }

    /**
     * 判断是否为中评
     */
    public boolean isNeutralReview() {
        return rating != null && rating == 3;
    }

    /**
     * 判断是否有评价内容
     */
    public boolean hasContent() {
        return reviewContent != null && !reviewContent.trim().isEmpty();
    }
}
